export default class HelperDownload {
	constructor (game, url, autoDownload, autoInstall, downloadState, downloadedSize, totalSize, filename) {
	// constructor (options = {}) {
		this.game = game;
		this.url = url;
		this.autoDownload = autoDownload;
		this.autoInstall = autoInstall;
		this.downloadState = downloadState;
		this.downloadedSize = downloadedSize;
		this.totalSize = totalSize;
		this.filename = filename;
		this.downloadProcess = 0;
		if (this.totalSize) {
			this.downloadProcess = this.downloadedSize / this.totalSize * 100;
		}
		this.download = null;
		
		this.create();
		
	}
	onStateChange(func) {
		func(this)
	}
	create() {
		// #ifdef H5
		return {
			state: 0,
			process: 0,
			downloadedSize: 22222222,
			totalSize: 333333333333333,
		}
		// #endif
	
		// #ifdef APP
		// this.download = null;
		this.download = plus.downloader.createDownload(this.url, {}, (d, status) => {
			if (status != 200) {
				console.log("Download failed: " + status);
				uni.showToast({
					title: "下载失败,请检查网络",
					icon: "none",
					duration: 1500,
				});
				return;
			}
			if (this.autoInstall) {
				this.open();
			}
		});
		// 事件会有延迟
		this.download.addEventListener("statechanged", (d, status) => {
			if (status !== 200) return ;
			
			this.filename = d.filename;
			this.downloadProcess = parseInt(d.downloadedSize / d.totalSize * 100);
			this.downloadedSize = d.downloadedSize;
			this.totalSize = d.totalSize;
			
	
			if (this.downloadProcess > 10) {
				// this.pause();
			}
			// 下载完成 
			if (d.state == 4) {
				console.log("Download success: " + d.getFileName());
				this.downloadState = 4;
			}
			console.log("statechanged", this.downloadProcess, this.downloadState, d.state, status);
		});
		// this.downloadState = 0;
		// this.downloadProcess = 0;
		if (this.autoDownload) this.start();
		// #endif
	}
	start() {
		if (!this.download) {
			this.create();
		}
		this.downloadState = 3;
		this.download.start();
		console.log("start", this);
	}
	abort() {
		// 先暂停
		// this.pause();
	
		return new Promise((resolve, reject) => {
			// 安卓的确认在左边, 对调一下
			// #ifdef APP-ANDROID
			uni.showModal({
				title: '提示',
				content: '确认后将终止下载, 并删除该任务',
				cancelText: "确认",
				cancelColor: "#007aff",
				confirmText: "取消",
				confirmColor: "#000000",
				success: (res) => {
					if (res.confirm) {
						// 恢复下载
						// this.downloadState = 3;
						// this.resume();
						reject();
					} else if (res.cancel) {
						this.download.abort();
						resolve();
					}
				}
			});
			// #endif
			
			// #ifndef APP-ANDROID
			uni.showModal({
				title: '提示',
				content: '确认后将终止下载, 并删除该任务',
				success: (res) => {
					if (res.confirm) {
						// #ifdef APP-PLUS
						this.download.abort();
						// #endif
						resolve();
					} else if (res.cancel) {
						// 恢复下载
						// this.downloadState = 3;
						// #ifdef APP-PLUS
						// this.resume();
						// #endif
						reject();
					}
				}
			});
			// #endif
		})
		
		console.log("downloadAbort", this)
	}
	pause() {
		this.download.pause();
		this.downloadState = 5;
		console.log("downloadPause", this)
	}
	resume() {
		this.download.resume();
		this.downloadState = 3;
		console.log("downloadResume", this)
	}
	open() {
		// TODO 文件可能已删除
		if (this.filename) {
			uni.openDocument({
				filePath: this.filename,
				success: (res) => {
					console.log(res, "打开安装包")
				}
			})
		}
	}
	isPause() {
		return this.downloadState === 5;
	}
	isStart() {
		return [1, 2, 3].includes(this.downloadState);
	}
	isInit() {
		return this.downloadState === 0;
	}
	isSuccess() {
		return this.downloadState === 4 && this.filename;
	}
	getDownloadedSize() {
		return this.downloadedSize || 0;
	}
	getTotalSize() {
		return this.totalSize || 0;
	}
	getDownloadProcess() {
		return this.downloadProcess || 0;
	}
	// btnText() {
	// 	console.log("btnText", this.downloadState)
	// 	if (this.downloadState === 0) {
	// 		return "下载";
	// 	} else if (this.downloadState > 0 && this.downloadState < 3) {
	// 		return "暂停";
	// 	} else if (this.downloadState === 4) {
	// 		return "打开";
	// 	} else if (this.downloadState === 5) {
	// 		return "继续";
	// 	}
	// }
	// btnAction() {
	// 	console.log("btnAction", this.downloadState)
	// 	if (this.downloadState === 0) {
	// 		this.start();
	// 	} else if (this.downloadState > 0 && this.downloadState < 3) {
	// 		this.pause();
	// 	} else if (this.downloadState === 4) {
	// 		this.open();
	// 	} else if (this.downloadState === 5) {
	// 		this.resume();
	// 	}
	// 	return false;
	// }
}